Ext.ns("CargoAdminView");
CargoAdminView = Ext.extend(Ext.Panel, {
	searchPanel : null,
	gridPanel : null,
	store : null,
	topbar : null,
	layout : "form",
	constructor : function(a) {
		Ext.applyIf(this, a);
		this.initUIComponents();
		CargoAdminView.superclass.constructor.call(this, {
					id : "CargoAdminView",
					title : "包类型列表",
					iconCls : "menu-car_apply",
					items : [this.searchPanel, this.gridPanel]
				});
	},
	initUIComponents : function() {
		this.searchPanel = new Ext.FormPanel({
			id : "CargoAdminSearchForm",
			height : 40,
			region : "north",
			frame : false,
			border : false,
			layout : "hbox",
			layoutConfig : {
				padding : "5",
				align : "middle"
			},
			defaults : {
				xtype : "label",
				margins : {
					top : 0,
					right : 4,
					bottom : 4,
					left : 4
				}
			},
			items : [{
						text : "请输入查询条件:"
					}, {
						text : "类型名称"
					}, {
						xtype : "textfield",
						name : "cargo.cargo_name"
					}, {
						xtype : "button",
						text : "查询",
						iconCls : "btn-search",
						handler : function() {
							var c = Ext.getCmp("CargoAdminSearchForm");
							var d = Ext.getCmp("CargoGrid");
							if (c.getForm().isValid()) {
								c.getForm().submit({
									waitMsg : "正在提交查询",
									url : _ctxPath + "listCargo.do",
									success : function(f, g) {
										alert(g.response.responseText)
										var e = Ext.util.JSON
												.decode(g.response.responseText);
										d.getStore().loadData(e);
									},
									failure : function(f, g) {
										var e = Ext.util.JSON
												.decode(g.response.responseText);
										d.getStore().loadData(e);
									}
								});
							}
						}
					}]
		});
		this.store = new Ext.data.JsonStore({
					autoLoad : true,
					url : 'listCargo.do',
					paramNames : {
						start : 'start',
						limit : 'rownum'
					},// 重命名分页参数以和action中对象对应
					root : 'list',
					totalProperty : 'totalCount',
					idProperty : 'id',
					remoteSort : true,
					fields : ['id', 'cargo_name', 'cargo_unit', 'cargo_memo']
				});
		this.store.setDefaultSort('id', 'desc');
		var b = new Ext.grid.CheckboxSelectionModel();
		var a = new Ext.grid.ColumnModel({
			columns : [b, {
						header : "货品名称",
						dataIndex : 'cargo_name',
						width : 100,
						sortable : true
					}, {
						header : "单位",
						dataIndex : 'cargo_unit',
						width : 100,
						sortable : true
					}, {
						header : "备注",
						dataIndex : 'cargo_memo',
						width : 100,
						align : 'left',
						sortable : true
					}, {
						header : "管理",
						dataIndex : "id",
						width : 50,
						sortable : false,
						renderer : function(g, f, d, j, e) {
							var i = d.data.id;
							var h = "";
							if (isGranted("app.CargoAction.delCargo")) {
								h = '<button title="删除" value=" " class="btn-del" onclick="CargoAdminView.remove('
										+ i + ')">&nbsp;</button>';
							}
							if (isGranted("app.CargoAction.saveCargo")) {
								h += '&nbsp;<button title="编辑" value=" " class="btn-edit" onclick="CargoAdminView.edit('
										+ i + ')">&nbsp;</button>';
							}
							return h;
						}
					}],
			defaults : {
				sortable : true,
				menuDisabled : false,
				width : 100
			}
		});
		this.topbar = new Ext.Toolbar({
					id : "CargoAdminFootBar",
					height : 30,
					bodyStyle : "text-align:left",
					items : []
				});
		if (isGranted("app.CargoAction.saveCargo")) {
			this.topbar.add(new Ext.Button({
						iconCls : "btn-add",
						text : "添加货品",
						handler : function() {
							new CargoForm().show();
						}
					}));
		}
		if (isGranted("app.CargoAction.delCargo")) {
			this.topbar.add(new Ext.Button({
						iconCls : "btn-del",
						text : "删除货品",
						handler : function() {
							var e = Ext.getCmp("CargoGrid");
							var c = e.getSelectionModel().getSelections();
							if (c.length == 0) {
								Ext.Msg.alert("信息", "请选择要删除的记录！");
								return;
							}
							var f = Array();
							for (var d = 0; d < c.length; d++) {
								f.push(c[d].data.id);
							}
							CargoAdminView.remove(f.join());
						}
					}));
		}
		this.gridPanel = new Ext.grid.GridPanel({
					id : "CargoGrid",
					tbar : this.topbar,
					region : "center",
					store : this.store,
					trackMouseOver : true,
					disableSelection : false,
					loadMask : true,
					autoHeight : true,
					cm : a,
					sm : b,
					viewConfig : {
						forceFit : true,
						enableRowBody : false,
						showPreview : false
					},
					bbar : new Ext.PagingToolbar({
								pageSize : 25,
								store : this.store,
								displayInfo : true,
								displayMsg : "当前显示从{0}至{1}， 共{2}条记录",
								emptyMsg : "当前没有记录"
							})
				});
		this.gridPanel.addListener("rowdblclick", function(d, c, f) {
					d.getSelectionModel().each(function(e) {
								if (isGranted("app.CargoAction.saveCargo")) {
									CargoAdminView.edit(e.data.id);
								}
							});
				});
	}
});
CargoAdminView.remove = function(b) {
	var a = Ext.getCmp("CargoGrid");
	Ext.Msg.confirm("信息确认", "您确认要删除该记录吗？", function(c) {
				if (c == "yes") {
					Ext.Ajax.request({
								url : _ctxPath + "delCargo.do?op=ajax",
								params : {
									'ids' : b
								},
								method : "post",
								success : function(response, opts) {
									var res = Ext.decode(response.responseText);
									if (res.success) {
										Ext.ux.Toast.msg("信息提示", "成功删除所选记录！");
										a.getStore().reload({
													params : {
														start : 0,
														limit : 25
													}
												});
									} else {
										Ext.Msg.alert("信息提示", res.message);
									}
								}
							});
				}
			});
};
CargoAdminView.edit = function(a) {
	new CargoForm({
				id : a
			}).show();
};